package com.len.controller;

import com.alibaba.fastjson.JSON;
import com.len.entity.ReportConfig;
import com.len.service.ReportConfigService;
import com.len.service.ReportService;
import com.len.util.ReType;
import com.len.vo.ColsVo;
import com.len.vo.ParameterInfo;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xiangyang
 * @Date 2019-01-16
 */
@Controller
@RequestMapping("/showReport")
public class ReportController {

    @Autowired
    ReportService reportService;
    @Autowired
    ReportConfigService reportConfigService;



    @ApiOperation(value = "/getCloudChartsData", httpMethod = "POST", notes = "获取字符云数据")
    @PostMapping(value = "getCloudChartsData")
    @ResponseBody
    public String getCloudChartsData(ParameterInfo parameterInfo) throws IOException {
        String cloudChartsDataStr = reportService.getCloudChartsData(parameterInfo);
        return cloudChartsDataStr;
    }


    @ApiOperation(value = "/getLineChartsData", httpMethod = "POST", notes = "获取折线图数据")
    @PostMapping(value = "getLineChartsData")
    @ResponseBody
    public String getLineChartsData(ParameterInfo parameterInfo) throws IOException {
        String lineChartsDataStr = reportService.getLineChartsData(parameterInfo);
        return lineChartsDataStr;
    }


    /**
     * getReportMeasureByReportId
     * @param reportId
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "/getReportMeasureByReportId", httpMethod = "POST", notes = "获取表格clos")
    @PostMapping(value = "getReportMeasureByReportId")
    @ResponseBody
    public ReType getReportMeasureByReportId(String reportId) throws Exception {
        ReType reType = new ReType();
        List<Map<String, String>> data = reportService.getReportMeasureByReportId(reportId);
        reType.setData(data);
        return reType;
    }

    /**
     * 获取table数据cols
     * @param info
     * @returne
     * @throws Exception
     */
    @ApiOperation(value = "/getTableCols", httpMethod = "POST", notes = "获取表格clos")
    @PostMapping(value = "getTableCols")
    @ResponseBody
    public String getTableCols(ParameterInfo info) throws Exception {
        List<ColsVo> tableData = reportService.getTableCols(info);
        String closJsonStr = JSON.toJSONString(tableData);
        return closJsonStr;
    }

//    /**
//     * 获取table数据
//     * @param info
//     * @return
//     * @throws Exception
//     */
//    @ApiOperation(value = "/getDiffTableData", httpMethod = "POST", notes = "复杂sql表格数据")
//    @PostMapping(value = "getDiffTableData")
//    @ResponseBody
//    public ReType getDiffTableData(ParameterInfo info) throws Exception {
//        ReType reType = new ReType();
//        Map<String, Object> result = reportService.getDiffTableData(info);
//        reType.setCount(Long.parseLong(result.get("size").toString()));
//        reType.setMsg(result.size()<=0?"无数据": "数据响应成功");
//        List<LinkedHashMap<Object, Object>> datas = (List<LinkedHashMap<Object, Object>>) result.get("datas");
//        reType.setData(datas);
//        String cols = result.get("cols")== null?"":result.get("cols").toString();
//        reType.setCols(cols);
//        return reType;
//    }

    /**
     * 获取table数据
     * @param info
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "/getTableData", httpMethod = "POST", notes = "关键词表格数据")
    @PostMapping(value = "getTableData")
    @ResponseBody
    public ReType getTableData(ParameterInfo info) throws Exception {
        ReType reType = new ReType();
        List<LinkedHashMap<Object, Object>> tableData = reportService.getTableData(info);
        reType.setCount(tableData == null || tableData.size() < 0 ? 0L : tableData.size());
        reType.setMsg(tableData.size()<=0?"无数据": "数据响应成功");
        reType.setData(tableData);
        return reType;
    }

    @GetMapping("/showReport")
    @RequiresPermissions(value = "showReport:show")
    public String report(String reportId, Model model) {
        ReportConfig reportConfig = reportConfigService.selectByPrimaryKey(reportId);
        String chartType = reportConfig.getChartType();
        if("line".equals(chartType)){
            model.addAttribute("reportConfig", reportConfig);
            return "report/line";
        }
        if("cloud".equals(chartType)){
            model.addAttribute("reportConfig", reportConfig);
            return "report/cloud";
        }
        if("bar".equals(chartType)){
            model.addAttribute("reportConfig", reportConfig);
            return "report/bar";
        }

        if("table".equals(chartType)){
            model.addAttribute("reportConfig", reportConfig);
            return "report/table";
        }
        return null;
    }

    /**
     * 初始化QueryBuilder
     * @param reportId
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "/getQueryBuilderByReportId", httpMethod = "POST", notes = "获取QueryBuilder数据")
    @PostMapping(value = "getQueryBuilderByReportId")
    @ResponseBody
    public ReType getQueryBuilderByReportId(String reportId) throws Exception {
        ReType reType = new ReType();
        List<Map<String, Object>> queryBuilderData =  reportService.getQueryBuilderByReportId(reportId);
        reType.setData(queryBuilderData);
        return reType;
    }

}
